package com.zbh.uexam.utility;

import lombok.extern.slf4j.Slf4j;
import org.modelmapper.*;
import org.modelmapper.convention.MatchingStrategies;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
public class ModelMapperSingle {
    protected final static ModelMapper modelMapper = new ModelMapper();
    private final static ModelMapperSingle modelMapperSingle = new ModelMapperSingle();

    // static Provider<Date> dateProvider = new AbstractProvider<Date>() {
    //     @Override
    //     public Date get() {
    //         return new Date();
    //     }
    // };

    static Converter<String, Date> toStringDate = new AbstractConverter<String, Date>() {
        @Override
        protected Date convert(String source) {
            if (source == null) return null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = simpleDateFormat.parse(source);
            } catch (ParseException e) {
                log.error("日期转换错误",e);
            }
            return date;
        }
    };

    static {
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        modelMapper.addConverter(toStringDate);
    }

    public static ModelMapper Instance() {
        return modelMapperSingle.modelMapper;
    }
}
